<template>
  <div>
    <el-dialog title="编辑产品型号" :visible.sync="dialogVisible" width="500px" :before-close="closeEditDialog"
      :close-on-click-modal="false" :append-to-body="true">
      <el-form :model="editInfo" :rules="rules" ref="refAddInfo" label-width="110px">
        <el-form-item size="small" label="产品型号" prop="model">
          <el-input clearable v-model.trim="editInfo.model" placeholder="请输入产品型号" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item size="small" label="产品ID" prop="accessId">
          <el-input clearable v-model.trim="editInfo.accessId" placeholder="请输入产品ID" maxlength="8"
            oninput="value=value.replace(/[^\w_]/g,'');"></el-input>
        </el-form-item>
        <el-form-item size="small" label="产品密钥" prop="accessKey">
          <el-input clearable v-model.trim="editInfo.accessKey" placeholder="请输入产品密钥" maxlength="32"
            oninput="value=value.replace(/[^\w_]/g,'');"></el-input>
        </el-form-item>
        <el-form-item size="small" label="产品型号描述" prop="description">
          <el-input type="textarea" :rows="2" placeholder="请输入产品型号描述" maxlength="100"
            v-model.trim="editInfo.description" resize='none' :autosize="{ minRows: 2, maxRows: 5 }">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button size="small" class="confirm-button" @click="editSubmit" v-preventReClick="1000">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqProductModelUpdate } from "@/api/api";
import { mapState } from "vuex";

export default {
  name: "add",
  props: ["editInfo"],
  components: {},
  data() {
    return {
      dialogVisible: false,
      // 表单验证规则
      rules: {
        model: [
          { required: true, message: "请输入产品型号", trigger: "blur" },
        ],
        accessId: [
          { required: true, message: "请输入产品ID", trigger: "blur" },
        ],
        accessKey: [
          { required: true, message: "请输入产品密钥", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    // 点击
    async editSubmit() {
      this.$refs.refAddInfo.validate(async (valid) => {
        if (valid) {
          const res = await reqProductModelUpdate(this.editInfo);
          // console.log(res);
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
          this.$modal.notifySuccess("编辑产品型号成功");
          this.$parent.ProductModelPage();
          this.closeEditDialog();
        }
      });
    },
    // 点击关闭弹框
    closeEditDialog() {
      this.$refs.refAddInfo.resetFields();
      this.dialogVisible = false;
    },
  },
  mounted() {
  },
  created() {
  },
};
</script>

<style lang="less" scoped>
@import url("../../common.less");
</style>